package com.simen.downloader;

import com.simen.downloader.listener.DownloadListener;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * Created by zhangming on 16/4/26.
 */
final class Loader {
    private Reference<DownloadListener> downloadListener;

    public Loader(DownloadListener listener) {
        this.downloadListener = new WeakReference<DownloadListener>(listener);
    }

    public int getId() {
        DownloadListener listener = downloadListener.get();
        return listener == null ? super.hashCode() : listener.hashCode();
    }

    public DownloadListener get() {
        return this.downloadListener.get();
    }

    public boolean isCollected() {
        return this.downloadListener.get() == null;
    }

    public void reInit(DownloadListener listener) {
        this.downloadListener = new WeakReference<>(listener);
    }
}
